﻿using Production;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GXWA_DATA.Utils
{
    public static class Tools
    {
        public static Form1 UiForm { get; set; }

        /// <summary>
        /// 生产日志消息处理
        /// </summary>
        /// <param name="message"></param>
        public static async void ProduceLogText(string message)
        {
            try
            {
                await AsyncLogger.WriteLogAsync("ProduceLog", message);

                if (UiForm != null && !UiForm.IsDisposed && UiForm.InvokeRequired)
                {
                    UiForm.Invoke(new Action(() =>
                    {
                        ClearIfExceedMaxLine(UiForm.ProductionLogsText, 50000);
                        UiForm.ProductionLogsText.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "：" + message + "\n");
                    }));
                }
                else
                {
                    UiForm.ProductionLogsText.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "：" + message + "\n");
                }
            }
            catch (Exception e)
            {
                Console.Write(e);
            }
        }

        /// <summary>
        /// 生产日志消息报错处理
        /// </summary>
        /// <param name="message"></param>
        public static async void ProduceLogTextError(string message)
        {
            try
            {
                await AsyncLogger.WriteLogAsync("ProduceLog", message);

                //日志高亮
                if (UiForm != null && !UiForm.IsDisposed && UiForm.InvokeRequired)
                {
                    UiForm.Invoke(new Action(() =>
                    {
                        ClearIfExceedMaxLine(UiForm.ProductionLogsText, 50000);
                        AppendLogMessage(UiForm.ProductionLogsText, message);
                    }));
                }
                else
                {
                    AppendLogMessage(UiForm.ProductionLogsText, message);
                }
            }
            catch (Exception e)
            {
                Console.Write(e);
            }
        }

        /// <summary>
        /// 设备运行日志消息处理
        /// </summary>
        /// <param name="message"></param>
        public static async void DeviceLogText(string message)
        {
            try
            {
                await AsyncLogger.WriteLogAsync("Device", message);

                if (UiForm != null && !UiForm.IsDisposed && UiForm.InvokeRequired)
                {
                    UiForm.Invoke(new Action(() =>
                    {
                        ClearIfExceedMaxLine(UiForm.DeviceLogsText, 50000);

                        UiForm.DeviceLogsText.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "：" + message + "\n");
                    }));
                }
                else
                {
                    UiForm.DeviceLogsText.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "：" + message + "\n");
                }
            }
            catch (Exception e)
            {
                Console.Write(e);
            }
        }

        /// <summary>
        /// 设备日志消息报错处理
        /// </summary>
        /// <param name="message"></param>
        public static async void DeviceLogTextError(string message)
        {
            try
            {
                await AsyncLogger.WriteLogAsync("Device", message);

                //日志高亮
                if (UiForm != null && !UiForm.IsDisposed && UiForm.InvokeRequired)
                {
                    UiForm.Invoke(new Action(() =>
                    {
                        ClearIfExceedMaxLine(UiForm.DeviceLogsText, 50000);

                        AppendLogMessage(UiForm.DeviceLogsText, message);
                    }));
                }
                else
                {
                    AppendLogMessage(UiForm.DeviceLogsText, message);
                }
            }
            catch (Exception e)
            {
                Console.Write(e);
            }
        }

        /// <summary>
        /// 日志高亮处理
        /// </summary>
        /// <param name="richTextBox"></param>
        /// <param name="message"></param>
        public static void AppendLogMessage(RichTextBox richTextBox, string message)
        {
            message = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "：" + message;

            // 将消息添加到RichTextBox
            richTextBox.AppendText(message + Environment.NewLine);

            // 高亮显示刚添加的消息
            int startIndex = richTextBox.TextLength - message.Length - 1; // 计算消息的起始索引
            if (startIndex == -1)
            {
                startIndex = 0;
            }

            richTextBox.Select(startIndex, message.Length);
            richTextBox.SelectionColor = Color.Red;
            richTextBox.SelectionFont = new Font(richTextBox.Font, FontStyle.Bold);

            // 清除选择
            richTextBox.Select(richTextBox.TextLength, 0);
            // 重置颜色为默认
            richTextBox.SelectionColor = richTextBox.ForeColor;
            richTextBox.SelectionFont = richTextBox.Font;
        }

        /// <summary>
        /// 控制白板输出字符
        /// </summary>
        /// <param name="richTextBox"></param>
        /// <param name="maxLines"></param>
        private static void ClearIfExceedMaxLine(RichTextBox richTextBox, int maxLines)
        {
            if (richTextBox.TextLength > maxLines)
            {
                richTextBox.Clear();
            }
        }

        internal static void ToolControl(Form1 form1)
        {
            UiForm = form1;
        }

        /// <summary>
        /// 是默认时间返回true
        /// </summary>
        /// <param name="endTime"></param>
        /// <returns></returns>
        internal static bool IsDateTimeValid(DateTime? endTime)
        {
            return !(endTime.HasValue && endTime.Value.Year > 1990);
        }
    }
}
